package com.spera.genutil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Maps;
import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.UDate;
import com.spring.security.web.dao.JedisCache;
import com.spring.sperapc.web.dao.FamilyMemberMapper;
import com.spring.sperapc.web.model.FamilyMember;
import com.spring.tools.SpringContextHolder;

/**
 * @author wojunyu
 *
 */
public class TokenUtil {
	public static String USER_ID = "user_id";
	public static String USER_NAME = "user_name";
	public static String FAMILY_ID = "family_id";
	public static String ROLE_ID = "role_id";
	public static String MEMBER_NAME = "member_name";
	public static String DEPT_ID = "dept_id";
	public static String LOGIN_IP = "login_ip";
	public static String LOGIN_TIME = "login_time";
	public static String LOGIN_TYPE = "login_type";
	
	public static String CUS_LOGIN = "customer";
	public static String OA_LOGIN = "oa";
	public static String DEVICE_CATEGORY = "deviceCategory";
	
	private static FamilyMemberMapper familyMemberMapper = SpringContextHolder.getBean(FamilyMemberMapper.class);
	
	public static String SetSperaToken(String user_uuid,JedisCache jedisCache,Token token,String type){
//		String user_uuid = ApplicationUtils.randomUUID();		
		if(user_uuid==null){
			user_uuid = SpUtil.getSessionId();
		}
		
		Map<String,String> ms = Maps.newHashMap();
		if(StringUtils.isEmpty(token.getUser_id())||StringUtils.isEmpty(token.getUser_name())){
			return "";
		}
		ms.put(USER_ID, token.getUser_id());
		ms.put(USER_NAME, token.getUser_name());
		ms.put(LOGIN_TIME, UDate.dateToStr(new Date(), "yyyyMMddHHmmss"));
		ms.put(LOGIN_TYPE, type);
		ms.put(DEVICE_CATEGORY, token.getDeviceCategory());
		
		if(ApplicationUtils.isNotNull(token.getFamily_id())){
			ms.put(FAMILY_ID, token.getFamily_id());
		}
		if(ApplicationUtils.isNotNull(token.getRole_id())){
			ms.put(ROLE_ID, token.getRole_id());
		}
		if(ApplicationUtils.isNotNull(token.getMember_name())){
			ms.put(MEMBER_NAME, token.getMember_name());
		}
		if(ApplicationUtils.isNotNull(token.getDept_id())){
			ms.put(DEPT_ID, token.getDept_id());
		}
		if(ApplicationUtils.isNotNull(token.getLogin_ip())){
			ms.put(LOGIN_IP, token.getLogin_ip());
		}
		jedisCache.hmset(user_uuid, ms);
		jedisCache.expire(user_uuid, JedisCont.JEDIS_EXPRISE);
		return user_uuid;
	}
	
	public static String SetSperaToken(JedisCache jedisCache,Token token,Integer sec,String type){
		String user_uuid = ApplicationUtils.randomUUID();
		Map<String,String> ms = Maps.newHashMap();
		if(StringUtils.isEmpty(token.getUser_id())||StringUtils.isEmpty(token.getUser_name())){
			return "";
		}
		ms.put(USER_ID, token.getUser_id());
		ms.put(USER_NAME, token.getUser_name());
		ms.put(LOGIN_TIME, UDate.dateToStr(new Date(), "yyyyMMddHHmmss"));
		ms.put(LOGIN_TYPE, type);
		
		if(ApplicationUtils.isNotNull(token.getFamily_id())){
			ms.put(FAMILY_ID, token.getFamily_id());
		}
		if(ApplicationUtils.isNotNull(token.getRole_id())){
			ms.put(ROLE_ID, token.getRole_id());
		}
		if(ApplicationUtils.isNotNull(token.getMember_name())){
			ms.put(MEMBER_NAME, token.getMember_name());
		}
		if(ApplicationUtils.isNotNull(token.getDept_id())){
			ms.put(DEPT_ID, token.getDept_id());
		}
		if(ApplicationUtils.isNotNull(token.getLogin_ip())){
			ms.put(LOGIN_IP, token.getLogin_ip());
		}
		jedisCache.hmset(user_uuid, ms);
		jedisCache.expire(user_uuid, sec);
		return user_uuid;
	}
	
	
	public static Map<String, Object> checkToken(String token,JedisCache jedisCache) {
//		String user_uuid = SpUtil.getSessionId();
		Map<String, Object> s = new HashMap<String, Object>();
		if (!ApplicationUtils.isNotNull(token)) {
			s.put(ApplicationUtils.STATUS, "0");
			s.put(ApplicationUtils.MSG, "请先登录");
			return s;
		} else {
			if (!jedisCache.exists(token)) {
				s.put(ApplicationUtils.STATUS, "-9999");
				s.put(ApplicationUtils.MSG, "用户登录已失效，请先重新登录");
				return s;
			}			
			
			String phone=jedisCache.hget(token,TokenUtil.USER_NAME);
			String userID=jedisCache.hget(token,TokenUtil.USER_ID);
			
			Map<String,Object> param=new HashMap<String,Object>();
			param.put("c_user_id", userID);

			List<FamilyMember> familyMembers=familyMemberMapper.selectByFamilyId(param);
			FamilyMember familyMember=familyMembers.get(0);
			
			if(!familyMember.getPhone().equals(phone)){
				jedisCache.del(token);
				s.put(ApplicationUtils.STATUS, "-9999");
				s.put(ApplicationUtils.MSG, "用户登录已失效，请先重新登录");
				return s;
			}
				//刷新token
			jedisCache.expire(token, JedisCont.JEDIS_EXPRISE);
			return null;
			
		}
	}
}
